package com.zoo.diannaopeijian.service.impl;

import com.sun.tools.javah.Gen;
import com.zoo.diannaopeijian.comm.sql.GeneratorExampleBuilder;
import com.zoo.diannaopeijian.domain.Entryitem;
import com.zoo.diannaopeijian.domain.EntryitemExample;
import com.zoo.diannaopeijian.mapper.EntryitemMapper;
import com.zoo.diannaopeijian.service.EntryItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zx583 on 2018-01-27.
 */
@Service
public class EntryItemServiceImpl implements EntryItemService {
    @Resource
    private EntryitemMapper entryitemMapper;

    @Override
    public boolean insertEntryItem(Entryitem entryitem) {
        return entryitemMapper.insertSelective(entryitem) == 1;
    }

    @Override
    @Transactional
    public boolean insertEntryItems(List<Entryitem> entryitemList) {
        for (Entryitem e : entryitemList) {
            if (!insertEntryItem(e)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public List<Entryitem> deleteEntryItems(Entryitem entryitem) {
        EntryitemExample example = (EntryitemExample) GeneratorExampleBuilder.buildEqualExample(EntryitemExample.class, entryitem, false);

        if (example == null) {
            throw new RuntimeException("当前正欲执行清空数表操作");
        }

        List<Entryitem> itemBeDeleted = entryitemMapper.selectByExample(example);

        int numsBeDelete = entryitemMapper.deleteByExample(example);

        if (itemBeDeleted.size() != numsBeDelete) {
            // 要删除的数据被其他事务先删除了，要不要做什么东西呢
        }

        return itemBeDeleted;
    }

    @Override
    public List getEntryItemsByRecordId(Integer entryrecordid) {
        return entryitemMapper.getEntryItemsByRecordId(entryrecordid);
    }
}
